package ssm.blog.controller.admin;


import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import ssm.blog.entity.Quotation;
import ssm.blog.service.QuotationService;
import ssm.blog.util.ResponseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: lyon
 * @description: 语录控制层
 * @author: Lyon
 * @create: 2018-06-04 12:07
 **/
@Controller
@RequestMapping("/admin")
public class AdminQuotationController {
    @Resource
    QuotationService quotationService;
    @RequestMapping("/quotationsAdd")
    String quotationAdd(){
        return "interface/admin/quotationsAdd";
    }
    @RequestMapping("/addQuotation")
    String addQuotation(Quotation quotation, HttpServletResponse response) throws Exception {
        int resultRows=quotationService.add(quotation);
        JSONObject result=new JSONObject();
        if(resultRows>0){
            result.put("success",true);
        }else{
            result.put("success",false);
        }
        ResponseUtil.write(response,result);
        return null;
    }
    @RequestMapping("/quotationEdit")
    String quotationEdit(Model model,Integer id){
        Quotation quotation=quotationService.selectById(id);
        model.addAttribute("quotation",quotation);
        return "interface/admin/quotationsEdit";
    }
    @RequestMapping("/updateQuotation")
    String updateQuotation(Quotation quotation,HttpServletResponse response) throws Exception {
        int resultRows=quotationService.updateById(quotation);
        JSONObject result=new JSONObject();
        if(resultRows>0){
            result.put("success",true);
        }else{
            result.put("success",false);
        }
        ResponseUtil.write(response,result);
        return null;
    }
    @RequestMapping("/quotationDele")
    String quotationDele(Integer id,HttpServletResponse response)throws Exception{
        int resultRows=quotationService.deleById(id);
        JSONObject result=new JSONObject();
        if(resultRows>0){
            result.put("success",true);
        }else{
            result.put("success",false);
        }
        ResponseUtil.write(response,result);
        return null;
    }
    @RequestMapping("quotationsDele")
    String quotationsDele(HttpServletResponse response,String ids)throws Exception{
        String[] IDS_str=ids.split(",");
        //转化为Integer数组
        List<Integer> IDS_INT=new ArrayList<>();
        for(String i:IDS_str) {
            IDS_INT.add(Integer.valueOf(i));
        }
        //删除
        int resultRows=quotationService.deles(IDS_INT);
        //返回删除结果
        JSONObject result=new JSONObject();
        if(resultRows>0){
            result.put("success",true);
        }else {
            result.put("success",false);
        }
        ResponseUtil.write(response,result);
        return null;
    }
}
